<template>
  <div class="food-detail-page">
    <!-- 返回按钮 -->
    <router-link to="/shiwureliangchaxun" class="back-btn">
      <i class="fa fa-arrow-left"></i> 返回食物列表
    </router-link>
    
    <h1 class="detail-title">
      {{ foodDetail.name || '食物详情' }}
      <span v-if="loading">（加载中...）</span>
    </h1>
    
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <i class="fa fa-spinner fa-spin fa-2x"></i>
    </div>
    
    <!-- 错误提示 -->
    <div v-else-if="error" class="error-container">
      <i class="fa fa-exclamation-triangle fa-2x"></i>
      <p>{{ error }}</p>
    </div>
    
    <!-- 详情内容 -->
    <div v-else class="detail-content">
      <div class="food-info-container">
        <!-- 图片区域 -->
        <div class="food-image-section">
          <img 
            :src="foodDetail.url || defaultImage" 
            alt="食物图片" 
            class="food-image"
            @error="handleImageError"
          >
          <p v-if="imageError" class="image-error">图片加载失败，显示默认图</p>
          
          <!-- 新增分类标签 -->
          <div class="category-badge" :class="`badge-${getCategoryColor(foodDetail.food_category)}`">
            {{ foodDetail.food_category }}
          </div>
        </div>
        
        <!-- 营养成分区域 -->
        <div class="nutritional-info">
          <h2>营养成分</h2>
          <div class="nutrition-list">
            <div class="nutrition-item">
              <span class="label">卡路里：</span>
              <span class="value">{{ foodDetail.calories || '-' }} kcal</span>
            </div>
            <div class="nutrition-item">
              <span class="label">蛋白质：</span>
              <span class="value">{{ foodDetail.protein || '-' }} g</span>
            </div>
            <div class="nutrition-item">
              <span class="label">脂肪：</span>
              <span class="value">{{ foodDetail.fat || '-' }} g</span>
            </div>
            <div class="nutrition-item">
              <span class="label">碳水化合物：</span>
              <span class="value">{{ foodDetail.carbohydrate || '-' }} g</span>
            </div>
          </div>
          
          <!-- 新增描述区域 -->
          <div class="food-description">
            <h3>食物描述</h3>
            <p>{{ foodDetail.description || '暂无描述' }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import axios from 'axios';
import { useRoute } from 'vue-router';

const route = useRoute();
const foodDetail = ref({});
const loading = ref(true);
const error = ref(null);
const imageError = ref(false);
const defaultImage = 'https://picsum.photos/400/300?grayscale';

const handleImageError = () => {
  imageError.value = true;
};

onMounted(async () => {
  try {
    const foodId = route.params.id;
    if (!foodId) throw new Error('未提供食物ID');
    
    const response = await axios.get(`http://localhost:8080/MDMS0214_war/food/${foodId}`);
    foodDetail.value = response.data;
  } catch (err) {
    error.value = err.message || '获取食物详情失败，请检查网络';
    console.error('详情加载失败:', err);
  } finally {
    loading.value = false;
  }
});

// 根据分类返回不同颜色
const getCategoryColor = (category) => {
  const colors = {
    '水果': 'primary',
    '蔬菜': 'success',
    '主食': 'warning',
    '肉类': 'danger',
    '其他': 'secondary'
  };
  return colors[category] || 'primary';
};
</script>

<style scoped>
/* 全局动画变量 */
:root {
  --primary-color: #4CAF50;
  --secondary-color: #6c757d;
  --border-color: #e9ecef;
  --bg-light: #f8f9fa;
  --transition-speed: 0.3s;
}

.food-detail-page {
  font-family: 'Segoe UI', sans-serif;
  max-width: 800px;
  margin: 0 auto;
  padding: 30px;
  background-color: white;
  min-height: 100vh;
}

/* 返回按钮样式 */
.back-btn {
  display: inline-flex;
  align-items: center;
  color: #343a40;
  text-decoration: none;
  margin-bottom: 30px;
  font-size: 18px;
  padding: 8px 15px;
  border-radius: 4px;
  transition: all var(--transition-speed);
  background-color: #f0f9eb;
  border: 2px solid #d0f0c0;
}

.back-btn:hover {
  background-color: #007bff;
  border-color: #007bff;
  color: white;
  transform: translateX(3px);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.back-btn .fa-arrow-left {
  margin-right: 8px;
  color: #66bb6a;
}

.back-btn:hover .fa-arrow-left {
  color: white;
  transform: translateX(2px);
}

.detail-title {
  margin-bottom: 35px;
  display: flex;
  align-items: center;
  gap: 15px;
  font-size: 24px;
}

.loading-container,
.error-container {
  height: 80vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  gap: 20px;
  color: #6c757d;
}

.error-container {
  color: #dc3545;
}

.detail-content {
  line-height: 1.6;
  color: #495057;
}

/* 容器样式 */
.food-info-container {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 50px;
  margin: 40px 0;
}

/* 图片区域样式 */
.food-image-section {
  flex: 1;
  text-align: center;
  border: 2px solid #e9ecef;
  border-radius: 12px;
  padding: 30px;
  background-color: #f8f9fa;
  position: relative;
}

.food-image {
  max-width: 100%;
  max-height: 300px;
  object-fit: contain;
  transition: transform 0.3s;
  border-radius: 8px;
}

.food-image:hover {
  transform: scale(1.05);
}

.image-error {
  color: #6c757d;
  font-size: 1em;
  margin-top: 15px;
}

/* 分类标签样式 */
.category-badge {
  display: inline-block;
  padding: 6px 16px;
  margin-top: 20px;
  border-radius: 16px;
  font-size: 14px;
  font-weight: 500;
  text-transform: uppercase;
}

.badge-primary { background-color: #e3f2fd; color: #0d47a1; }
.badge-success { background-color: #e8f5e9; color: #1b5e20; }
.badge-warning { background-color: #fff3e0; color: #e65100; }
.badge-danger { background-color: #ffebee; color: #b71c1c; }
.badge-secondary { background-color: #f5f5f5; color: #424242; }

/* 营养成分区域样式 */
.nutritional-info {
  flex: 1;
  border-top: none;
  padding-top: 0;
  margin-bottom: 0;
}

.nutrition-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  margin: 20px 0;
}

.nutrition-item {
  display: flex;
  gap: 10px;
}

.label {
  font-weight: 500;
  color: #343a40;
  min-width: 120px;
  font-size: 16px;
}

.value {
  color: #212529;
  font-size: 16px;
}

/* 食物描述区域样式 */
.food-description {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e9ecef;
}

.food-description h3 {
  color: #343a40;
  margin-bottom: 10px;
}

.food-description p {
  line-height: 1.8;
  color: #6c757d;
}

/* 响应式调整 */
@media (max-width: 600px) {
  .food-info-container {
    flex-direction: column;
    gap: 30px;
  }
  
  .food-image-section {
    max-width: 100%;
  }
  
  .food-image {
    max-width: 100%;
    max-height: 250px;
  }
  
  .nutrition-list {
    grid-template-columns: 1fr;
  }
}
</style>